package com.itmk.web.user.controller;

import com.itmk.resp.Login;
import com.itmk.resp.Payload;
import com.itmk.utils.JWTUtils;
import com.itmk.utils.Result;
import com.itmk.utils.UID;
import com.itmk.web.adminInfo.entity.AdminInfo;
import com.itmk.web.ai.entity.Pdfs;
import com.itmk.web.companyInfo.entity.Company;
import com.itmk.web.user.entity.UserLogin;
import com.itmk.web.user.service.UserService;
import com.itmk.web.userInfo.entity.BasicInfo;
import com.itmk.web.userInfo.entity.EducationInfo;
import com.itmk.web.userInfo.entity.ForjobInfo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public Login login(@RequestBody UserLogin userLogin){
        //获取用户信息
        System.out.println(userLogin.getPhone());
        System.out.println(userLogin.getSignType());
        UserLogin userLogin1 = userService.getUser(userLogin.getPhone());
        Login login = new Login();
        if(userLogin1!=null&&userLogin1.getPassword().equals(userLogin.getPassword())&&userLogin1.getSignType().equals(userLogin.getSignType())){
            login.setCode(200);
            login.setName(userLogin1.getPhone());
            login.setMsg("登录成功！");
            userService.addUUID(userLogin1.getUuid());
            Payload payload = new Payload();
            payload.setUuid(userLogin1.getUuid());
            payload.setPhone(userLogin1.getPhone());
            //生成JWT令牌
            String token = JWTUtils.createJwt(payload);
            //将token保存进数据库
            login.setToken(token);
        }else if(userLogin1!=null&&!userLogin1.getPassword().equals(userLogin.getPassword())&&userLogin1.getSignType().equals(userLogin.getSignType())){
            login.setCode(401);
            login.setName("XXX");
            login.setMsg("登录失败！请检查账号密码是否正确");
        } else if (userLogin1!=null&&!userLogin1.getSignType().equals(userLogin.getSignType())) {
            login.setCode(402);
            login.setName("XXX");
            login.setMsg("登录身份出错");
        } else{
                //生成一个UUID，和其他信息现存进账号
                String uuid = UID.getUUID();
                userService.addUUID(uuid);
                userService.addUser(userLogin.getPhone(),userLogin.getPassword(),userLogin.getSignType(),uuid);

                //判断signType,再根据UUID来生成唯一账号。
                if(userLogin.getSignType().equals("geek")){

                    //个人简历有三部分组成
                    //1
                    BasicInfo basicInfo = new BasicInfo();
                    basicInfo.setPhone(userLogin.getPhone());
                    basicInfo.setPassword(userLogin.getPassword());
                    basicInfo.setSignType(userLogin.getSignType());
                    basicInfo.setUuid(uuid);
                    userService.addBasicInfo(basicInfo);
                    //2
                    EducationInfo educationInfo = new EducationInfo();
                    educationInfo.setUuid(uuid);
                    userService.addEducationInfo(educationInfo);
                    //3
                    ForjobInfo forjobInfo = new ForjobInfo();
                    forjobInfo.setUuid(uuid);
                    userService.addForjobInfo(forjobInfo);

                    //总的个人简历
                    Pdfs pdfs = new Pdfs();
                    pdfs.setUuid(uuid);
                    userService.addResume(pdfs);

                }else if(userLogin.getSignType().equals("boss")){

                    Company company = new Company();
                    company.setPhone(userLogin.getPhone());
                    company.setUuid(uuid);
                    userService.addCompany(company);

                }else{

                    AdminInfo adminInfo = new AdminInfo();
                    adminInfo.setPhone(userLogin.getPhone());
                    adminInfo.setUuid(uuid);
                    userService.addAdminInfo(adminInfo);

                }

                login.setCode(201);
                login.setName(userLogin.getPhone());
                login.setMsg("注册并登陆成功");
                Payload payload = new Payload();
                payload.setUuid(uuid);
                payload.setPhone(userLogin.getPhone());
                //生成JWT令牌
                String token = JWTUtils.createJwt(payload);
                login.setToken(token);
        }
        return login;
    }

    @RequestMapping ("check_token")
    @ResponseBody
    public Result checkToken(HttpServletRequest request){
       String token = request.getHeader("Authorization");
       Boolean result = JWTUtils.checkToken(token);
       if(result){
           return Result.ok().put("res",result);
       }else{
           return Result.error(token);
       }
    }

    /**
     * 设置登出信息
     * @return
     */
    @ResponseBody
    @RequestMapping("/logout")
    public Result logout(HttpServletRequest request){
        //解析唯一标识符
        String token = request.getHeader("Authorization");
        JwtParser jwtParser = Jwts.parser();
        String sign = JWTUtils.getSign();
        Jws<Claims> jwt = jwtParser.setSigningKey(sign).parseClaimsJws(token);
        Claims claims = jwt.getBody();
        String uuid = claims.get("uuid").toString();

        //销毁系统中指定的tokentoken
        Boolean res = userService.deleteUUID(uuid);
        if(res){
            return Result.ok("登出成功");
        }
        return Result.error("登出失败");
    }

    @RequestMapping("/html")
    public String test(){
        return "test.html";
    }
}
